#include <stdio.h>
#include <stdlib.h>
#include <string.h>



#define PICTURE_NUM		2190
#define BUFFSIZE		1024

const char* tar_name = "badapple_bmp_arr.c";



int main(int argc, char** argv)
{
	FILE* fp_src = NULL;
	char file_name[BUFFSIZE] = {0};
	int str_len;
	char ch;
	long file_end, file_real_end;

	//save all data to target file
	for(int i = 0; i < PICTURE_NUM; i++)
	{
		memset(file_name, 0, BUFFSIZE);
		sprintf(file_name, "%d", i + 1);
		str_len = strlen(file_name);
		file_name[str_len] = '.';
		file_name[str_len + 1] = 'c';
		file_name[str_len + 2] = '\0';

		if((fp_src = fopen(file_name, "r+")) == NULL)
		{
			printf("picture open error\n");
		}

		fseek(fp_src, 0, SEEK_END);
		file_end = ftell(fp_src);

		fseek(fp_src, 5, SEEK_END);
		file_real_end = ftell(fp_src);

		while(1)
		{
			fseek(fp_src, file_end--, SEEK_SET);
			fread(&ch, 1, 1, fp_src);

			if(ftell(fp_src) <= 20)
			{
				break;
			}

			fseek(fp_src, file_real_end--, SEEK_SET);
			fwrite(&ch, 1, 1, fp_src);

		}

		fseek(fp_src, 20, SEEK_SET);
		fwrite("code ", 1, 5, fp_src);

		fclose(fp_src);
	}


	exit(0);
}
